<?php

 
namespace RedMVC\Router;
use RedMVC;

/**
 * Rewrite class
 *  
 * @category    RedMVC
 * @package     Router
 * @author      Jan Fischer, bitWorking <info@bitworking.de>
 */
class Rewrite extends ARouter{

    public function readUrl(){
        $config         = RedMVC\Registry::get('RedMVC_Config');
        
        $pathInfo       = pathinfo($_SERVER['SCRIPT_NAME']);
        $dirName        = $pathInfo['dirname'];
        $dirName        = str_replace('/index.php', '', $dirName);
        $requestUri     = $_SERVER['REQUEST_URI'];
        $requestString  = substr($requestUri, strlen($dirName));
        // strip multiple slashes
        $requestString  = ltrim($requestString, '/');
        //$requestString  = preg_replace('~^/+|/+$|/(?=/)~', '', $requestString);
        $this->requestString = $requestString;
        
        unset($_GET['controller']);
        unset($_GET['action']);
        
        $this->params = array_merge($this->params, $_GET);

        // strip GET params
        if(strpos($requestString, '?') !== false){
            $requestString = strstr($requestString, '?', true);
        }
        if(empty($requestString)){
            $this->controllerName   = $config->defaultController;
            $this->actionName       = $config->defaultAction;
            return;
        }
        $requestArray = explode('/', $requestString);
    
        if(count($requestArray) == 1){
            $this->controllerName   = $requestArray[0];
            // translate
            $this->controllerName   = $this->__($this->controllerName);
            $this->actionName       = 'index';
            return;
        }
        
        $this->controllerName   = $requestArray[0];
        $this->actionName       = $requestArray[1];
        
        if('' == $this->actionName){
            $this->actionName = 'index';
        }
        
        // translate
        $this->controllerName   = $this->__($this->controllerName);
        $this->actionName       = $this->__($this->actionName);
        
        if(count($requestArray) > 2){
            $requestArray       = array_slice($requestArray, 2);
            $requestArrayCount  = count($requestArray);
            for($i=0;$i<$requestArrayCount;$i+=2){
                if(isset($requestArray[$i+1])){
                    $this->params[$requestArray[$i]] = urldecode($requestArray[$i+1]);
                }
                else{
                    $this->params[$requestArray[$i]] = null;
                }
            }
        }
        $this->params = array_merge($this->params, $_GET);
    }
    
    public function buildUrl($controller, $action, $params = array()){
        $paramsString = '';
        if(count($params) > 0){
            $paramsString = http_build_query($params, '', '/');
            $paramsString = '/'.str_replace('=', '/', $paramsString);            
        }
        if($paramsString == ''){
            if($controller == 'index' && $action == 'index'){
                return '/';
            }
            else if($action == 'index'){
                $controller = $this->_($controller);
                return "/$controller";
            }
        }
        // translate
        $controller = $this->_($controller);
        $action     = $this->_($action);        
        return "/$controller/$action$paramsString";
    }

}